const url_config = require('common/config');
var that;
App({
  globalData: {
    userInfo: {
      openid_wx: '',
      uid: '',
      head_img: '',
      nick_name: '',
      gender: 0,
      token: '',
    }
  },

  onLaunch: function() {
    that = this;
  },

  modifUserinfo: function(wxUserinfo, cb) {
    wx.request({
      url: url_config.url_modifyUserInfo,
      data: {
        nick_name: wxUserinfo.nickName,
        gender: wxUserinfo.gender,
        head_img: wxUserinfo.avatarUrl,
        country: wxUserinfo.country,
        province: wxUserinfo.province,
        city: wxUserinfo.city,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'uidusernamekey': that.globalData.userInfo.token
      },
      success: function(res) {
        let resStatus = res.data.status;
        let resMsg = res.data.message;
        //对反馈结果进行判断
        if (resStatus != 200) {
          wx.showModal({
            title: '提示',
            content: resMsg,
          })
          typeof cb == "function" && cb(false)
          return;
        }
        that.globalData.userInfo.nick_name = wxUserinfo.nickName;
        that.globalData.userInfo.gender = wxUserinfo.gender;
        that.globalData.userInfo.head_img = wxUserinfo.avatarUrl;
        typeof cb == "function" && cb(true)
      },
      fail: function() {
        wx.showModal({
          title: '提示',
          content: '网络异常',
        })
        typeof cb == "function" && cb(false)
      }
    })
  },

  getUserinfoByCode: function(cb) {
    wx.login({
      success(res) {
        if (res.code) {
          //发起网络请求
          that.requestUserInfoByCode(res.code, cb);
        } else {
          wx.showModal({
            title: '提示',
            content: '获取用户登陆信息失败，请退出重试',
          })
        }
      },
      fail: function() {
        wx.showModal({
          title: '提示',
          content: '获取用户登陆信息失败',
        })
      }
    })
  },
  requestUserInfoByCode: function(loginCode, cb) {
    //如果userinfo的 openid_wx 不为空的话就返回已有的用户信息
    if (that.globalData.userInfo.openid_wx) {
      typeof cb == "function" && cb(that.globalData.userInfo)
      return;
    }
    let systemInfo = wx.getSystemInfoSync();
    wx.request({
      url: url_config.url_getUserInfoByWxOpenid,
      data: {
        'loginCode': loginCode,
        'wx_sdkversion': systemInfo.SDKVersion
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        let resData = res.data;
        if (resData.status != 200) {
          wx.showModal({
            title: '提示',
            content: resData.message,
          })
          return;
        }
        that.globalData.userInfo = resData.data;
        console.log(that.globalData.userInfo)
        typeof cb == "function" && cb(that.globalData.userInfo)
      },
      fail: function() {
        wx.showModal({
          title: '提示',
          content: '网络异常',
        })
      }
    })
  },
  // ============将用户的formid上传到服务端
  uploadFormId(formid) {
    if (!that.globalData.userInfo.uid) {
      return;
    }
    wx.request({
      url: url_config.url_addWXformId,
      data: {
        uid: that.globalData.userInfo.uid,
        openid: that.globalData.userInfo.openid_wx,
        formid: formid,
        expire: parseInt(new Date().getTime() / 1000) + 604800 //计算7天后的过期时间的时间戳
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'uidusernamekey': that.globalData.userInfo.token
      },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        console.log(res);

      }

    })
  },
  //版本对比
  compareVersion: function(v1, v2) {
    v1 = v1.split('.')
    v2 = v2.split('.')
    var len = Math.max(v1.length, v2.length)

    while (v1.length < len) {
      v1.push('0')
    }
    while (v2.length < len) {
      v2.push('0')
    }

    for (var i = 0; i < len; i++) {
      var num1 = parseInt(v1[i])
      var num2 = parseInt(v2[i])

      if (num1 > num2) {
        return 1
      } else if (num1 < num2) {
        return -1
      }
    }
    return 0
  }

})